import Vue from "vue"

import VueRouter from "vue-router"

import store from "@/store"

Vue.use(VueRouter)

const Home = () => import("@pages/Home")
const Login = () => import("@pages/Login")
const Search = () => import("@pages/Search")
const Register = () => import("@pages/Register")
const Detail = () => import("@pages/Detail")
const AddCartSuccess = () => import("@pages/AddCartSuccess")
const ShopCart = () => import("@pages/ShopCart")
const Center = () => import("@pages/Center")
const Pay = () => import("@pages/Pay")
const PaySuccess = () => import("@pages/PaySuccess")
const Trade = () => import("@pages/Trade")

const last = VueRouter.prototype.push

VueRouter.prototype.push = function (location, onComplete = () => {}, onAbort = () => {}) {
    last.call(this, location, onComplete, onAbort)
}

const router = new VueRouter({
    scrollBehavior() {
        return {
            x: 0,
            y: 0
        }
    },
    mode: "history",
    routes: [{
            path: "/home",
            component: Home,
            name: 'home'
        },
        {
            path: "/login",
            component: Login,
            name: 'login',
            meta: {
                isShow: true
            },
            beforeEnter(to,from,next){
                if(store.state.user.token){
                    next('/')
                }else{
                    next()
                }
            }
        },
        {
            path: "/search/:keyword?",
            component: Search,
            name: 'search'

        },
        {
            path: "/register",
            component: Register,
            name: 'register',
            meta: {
                isShow: true
            }
        },
        {
            path: "/detail/:skuId",
            component: Detail,
            name: 'detail',
        },
        {
            path: "/addcartsuccess",
            component: AddCartSuccess,
            name: 'addcartsuccess',
        },
        {
            path: "/shopcart",
            component: ShopCart,
            name: 'shopcart',
        },
        {
            path: "/center",
            component: Center,
            name: 'center',
        },
        {
            path: "/pay",
            component: Pay,
            name: 'pay',
        },
        {
            path: "/paysuccess",
            component: PaySuccess,
            name: 'paysuccess',
        },
        {
            path: "/trade",
            component: Trade,
            name: 'trade',
        },
        {
            path: "/",
            redirect: "/home"
        },
    ]
})
//路由守卫进行权限校验
router.beforeEach((to, from, next) => {
    //需要进行校验的路由
    const checkedPath = ['/center', "/pay", "/paysuccess", "/trad","/shopcart"]
    const isChecked = checkedPath.some(item => {
        return to.path.startsWith(item)
    })
    //如果需要进行权限控制
    if (isChecked) {
        //是否有用户名,说明已经登录
        if (store.state.user.userName) {
            next()
        } else {
            //是否有token
            if (store.state.user.token) {
                //有token则获取用户名后再放行
                store.dispatch("user/getUserName")
                next()
            }else{
                next('/login')
            }
        }
        
    } else {
        next()
    }
})

export default router